//
//  AllQJViewController.m
//  svqj
//
//  Created by xftt on 14-6-23.
//
//

#import "SortListViewController.h"
#import "QJView.h"
#import "ShowViewController.h"
#import "SortMenuView.h"
#import "NewWebViewController.h"

@interface SortListViewController ()
{
    NSMutableArray *_QJData;
    UIScrollView *_scroll;
    int _type;
    BOOL isEdit;
}

@end

@implementation SortListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _scroll = [[UIScrollView alloc]initWithFrame:_contentView.bounds];
    [_contentView addSubview:_scroll];
    [_editButton setHidden:NO];
    [_editButton addTarget:self action:@selector(doEdit) forControlEvents:UIControlEventTouchUpInside];
}

-(void)viewWillAppear:(BOOL)animated
{
    if (isEdit) {
        [self doEdit];
    }
    [self reloadList];
}

- (void)reloadList
{
    [self loadSortData:_sortInfo];
}

- (void)loadSortData:(NSDictionary*)sortinfo
{
    [_backView setHidden:NO];
    UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doBack)];
    [_backView addGestureRecognizer:reg];
    
    _QJData = [NSMutableArray array];
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    NSArray *_AllQJ = [_Data objectForKey:@"QJData"];
    [_lbTitle setText:[sortinfo objectForKey:@"sortname"]];
    
    NSString *sortid = [sortinfo objectForKey:@"sortid"];
    for (int i=0; i<[_AllQJ count]; i++) {
        NSDictionary *dic = [_AllQJ objectAtIndex:i];
        if ([[dic objectForKey:@"sortid"] isEqualToString:sortid]) {
            [_QJData addObject:dic];
        }
    }
    [self loadQJList];
}

- (void)loadQJList
{
    [_scroll removeSubviews];
    for (int i=0; i<[_QJData count]; i++) {
        QuanJing *qj = [[QuanJing alloc]initWithDictionary:[_QJData objectAtIndex:i]];
        [_scroll addSubview:[self getViewWithQuanJing:qj AndIndex:i]];
    }
    CGSize size = CGSizeMake(_scroll.frame.size.width, (([_QJData count]-1)/3+1)*350);
    [_scroll setContentSize:size];
}

- (UIView *)getViewWithQuanJing:(QuanJing*)qj AndIndex:(NSInteger)index
{
    NSInteger line = index/3;
    NSInteger row = index-line*3;
    
    QJView *_qjview = [[QJView alloc] initWithFrame:CGRectMake(42+262*row, 50+350*line, 236, 250)];
    [_qjview setQuanjing:qj];
    [_qjview setViewEdit:isEdit];
    
    UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doClick:)];
    [_qjview.imgvQJ addGestureRecognizer:reg];
    _qjview.imgvQJ.tag = index;
    _qjview.FController = self;
    return _qjview;
}

- (void)doBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doEdit
{
    isEdit = !isEdit;
    for(UIView*view in _scroll.subviews)
    {
        if ([view isKindOfClass:[QJView class]]) {
            [(QJView*)view setViewEdit:isEdit];
        }
    }
    
    if (isEdit) {
        [_editButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    else
    {
        for (id view in _contentView.subviews) {
            if ([view isKindOfClass:[SortMenuView class]]) {
                [view removeFromSuperview];
            }
        }
        [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
    }
}

- (void)doClick:(UITapGestureRecognizer*)sender
{
    QuanJing *qj = [[QuanJing alloc] initWithDictionary:[_QJData objectAtIndex:sender.view.tag]];
    
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    NSMutableArray *_QJTemp = [NSMutableArray arrayWithArray:[_Data objectForKey:@"QJTemp"]];
    
    BOOL isNew = YES;
    for (NSDictionary *qjdictionary in _QJTemp) {
        if([[qjdictionary objectForKey:@"sid"] isEqualToString:qj.sid])
        {
            [_QJTemp removeObject:qjdictionary];
            [_QJTemp insertObject:qjdictionary atIndex:0];
            isNew = NO;
            break;
        }
    }
    
    if (isNew) {
        [_QJTemp insertObject:qj.dic atIndex:0];
        if ([_QJTemp count] > 6) {
            [_QJTemp removeLastObject];
        }
    }
    [_Data setObject:_QJTemp forKey:@"QJTemp"];
    [_Data synchronize];
    
    
    ShowViewController *show = [[ShowViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:show animated:YES completion:nil];
    [show setQj:qj];
}

- (void)saveSort
{
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    [_Data setValue:_QJData forKey:@"QJData"];
    [_Data synchronize];
}

-(void)selectSortMenu:(NSMutableDictionary*)quanjing
{
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    NSArray* QJSort = [_Data objectForKey:@"QJSort"];
    SortMenuView *sortMenu = [[SortMenuView alloc]initWithFrame:_contentView.bounds];
    [sortMenu setFController:self];
    [sortMenu setQuanjing:quanjing];
    [sortMenu setQJSort:QJSort];
    [_contentView addSubview:sortMenu];
}

@end
